package com.jojo.demo;

import com.jojo.demo.utils.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @ClassName: SendTopicDemo
 * @Description: TODO
 * @Author reyJojo
 * @Date 2021/11/30 9:51
 * @Version 1.0
 */
public class SendTopicDemo {
    private final static String EXCHANGE_NAME = "test_topic_exchange";

    public static void main(String[] argv) throws Exception {
        // 获取到连接
        Connection connection = ConnectionUtil.getConnection();
        // 获取通道
        Channel channel = connection.createChannel();
        // 声明exchange，指定类型为topic
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        // 消息内容
        String message = "这是一只行动迅速的橙色的兔子";
        // 发送消息，并且指定routing key为：quick.orange.rabbit
        channel.basicPublish(EXCHANGE_NAME, "quick.yellow.rabbit", null, message.getBytes());
        channel.basicPublish(EXCHANGE_NAME, "quick.orange.cat", null, message.getBytes());
        channel.basicPublish(EXCHANGE_NAME, "lazy.yellow.rabbit", null, message.getBytes());
        System.out.println(" [动物描述：] Sent '" + message + "'");

        channel.close();
        connection.close();
    }
}
